﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace de.codingfreaks.libs.cfThreadingTools.Extensions
{
    /// <summary>
    /// Provides extensions for <c>System.Windows.Forms.ProgressBar.</c>
    /// </summary>
    public static class ProgressBarExtensions
    {

        /// <summary>
        /// Thread-safe way to set the minimum-value of the <c>ProgressBar</c>.
        /// </summary>
        /// <param name="pbrTarget">The <c>System.Windows.Forms.ProgressBar</c>.</param>
        /// <param name="intValue">The new minimum-value.</param>
        public static void SetMinimumThreadSafe(this ProgressBar pbrTarget,
                                                int intValue)
        {
            BaseTools.SetPropertyValue(pbrTarget, "Minimum", intValue);
        }

        /// <summary>
        /// Thread-safe way to set the maximum-value of the <c>ProgressBar</c>.
        /// </summary>
        /// <param name="pbrTarget">The <c>System.Windows.Forms.ProgressBar</c>.</param>
        /// <param name="intValue">The new maximum-value.</param>
        public static void SetMaximumThreadSafe(this ProgressBar pbrTarget,
                                                int intValue)
        {
            BaseTools.SetPropertyValue(pbrTarget, "Maximum", intValue);
        }

        /// <summary>
        /// Thread-safe way to set the value of the <c>ProgressBar</c>.
        /// </summary>
        /// <param name="pbrTarget">The <c>System.Windows.Forms.ProgressBar</c>.</param>
        /// <param name="intValue">The new value.</param>
        public static void SetValueThreadSafe(this ProgressBar pbrTarget,
                                              int intValue)
        {
            if (intValue >= pbrTarget.Minimum &&
                intValue <= pbrTarget.Maximum)
            {
                // the operation should succeed
                BaseTools.SetPropertyValue(pbrTarget, "Value", intValue);
            }
            else
            {
                throw new ArgumentOutOfRangeException("The value has to be inside the range of minimum and maximum!");    
            }
        }
    }
}
